package itsm.isperp.workflow.cmd;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.module.service.bpm.BpmActionHistoryService;
import itsm.isperp.workflow.service.DelegateService;

import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.TaskEntity;

/**
 * 委托任务.
 */
public class DelegateTaskCmd implements Command<Void> {

	private String taskId;
	private String attorney;

	public DelegateTaskCmd(String taskId, String attorney) {
		this.taskId = taskId;
		this.attorney = attorney;
	}

	/**
	 * 委托任务.
	 * 
	 */
	public Void execute(CommandContext commandContext) {
		TaskEntity task = Context.getCommandContext().getTaskEntityManager()
				.findTaskById(taskId);

		String assignee = task.getAssignee();

		if (task.getOwner() == null) {
			task.setOwner(assignee);
		}

		task.setAssignee(attorney);
		ContextHolder.getSpringBean(DelegateService.class).saveRecord(assignee,
				attorney, taskId);

		return null;
	}
}
